package com.huangkailong.gitimgbed.view;

import com.google.inject.Inject;
import com.huangkailong.gitimgbed.core.ImgBedConfiguration;
import com.huangkailong.gitimgbed.core.ViewDispatcher;
import com.huangkailong.gitimgbed.service.JgitService;
import com.huangkailong.gitimgbed.viewmodel.SettingViewModel;
import de.saxsys.mvvmfx.FxmlView;
import de.saxsys.mvvmfx.InjectViewModel;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import lombok.extern.slf4j.Slf4j;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author huangkl
 * @since 1.0.0
 */
@Slf4j
public class SettingView implements FxmlView<SettingViewModel>, Initializable {
    @FXML
    private TextField textFieldUrl;
    @FXML
    private TextField textFieldBranch;
    @FXML
    private TextField textFieldSavePath;
    @FXML
    private TextField textFieldUserName;
    @FXML
    private TextField textFieldPasswd;
    @FXML
    private TextField textFieldImgUrlPrefix;
    @InjectViewModel
    private SettingViewModel settingViewModel;
    @Inject
    private ImgBedConfiguration imgBedConfiguration;
    @Inject
    private JgitService jgitService;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        initConfigInfo();
        bind();
    }

    @FXML
    public void handleConfirmBtn(ActionEvent e){
        updateConfig();
        jgitService.flushJgitConfig();
        ViewDispatcher.closeView(SettingView.class);
    }

    @FXML
    public void handleCancelBtn(ActionEvent e){
        ViewDispatcher.closeView(SettingView.class);
    }

    private void updateConfig() {
        imgBedConfiguration.setGitRepositoryUrl(settingViewModel.getGitRepositoryUrl());
        imgBedConfiguration.setGitRepositoryBranch(settingViewModel.getGitRepositoryBranch());
        imgBedConfiguration.setGitRepositoryLocalPath(settingViewModel.getGitRepositoryLocalPath());
        imgBedConfiguration.setGitUsername(settingViewModel.getGitUsername());
        imgBedConfiguration.setGitPasswd(settingViewModel.getGitPasswd());
        imgBedConfiguration.setImgUrlPrefix(settingViewModel.getImgUrlPrefix());
    }

    private void bind() {
        settingViewModel.gitRepositoryUrlProperty().bind(textFieldUrl.textProperty());
        settingViewModel.gitRepositoryBranchProperty().bind(textFieldBranch.textProperty());
        settingViewModel.gitRepositoryLocalPathProperty().bind(textFieldSavePath.textProperty());
        settingViewModel.gitUsernameProperty().bind(textFieldUserName.textProperty());
        settingViewModel.gitPasswdProperty().bind(textFieldPasswd.textProperty());
        settingViewModel.imgUrlPrefixProperty().bind(textFieldImgUrlPrefix.textProperty());
    }

    private void initConfigInfo(){
        textFieldUrl.setText(imgBedConfiguration.getGitRepositoryUrl());
        textFieldBranch.setText(imgBedConfiguration.getGitRepositoryBranch());
        textFieldSavePath.setText(imgBedConfiguration.getGitRepositoryLocalPath());
        textFieldUserName.setText(imgBedConfiguration.getGitUsername());
        textFieldPasswd.setText(imgBedConfiguration.getGitPasswd());
        textFieldImgUrlPrefix.setText(imgBedConfiguration.getImgUrlPrefix());
    }
}
